package com.jay.tools.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import androidx.core.view.MotionEventCompat;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.jay.tools.R;

import java.util.Arrays;
import java.util.List;

/**
 * author: tanjianpei
 * date: 2018/9/3 003 13:47
 * <p>
 * description: 快速索引列表
 */
public class QuickIndexBar extends View {

    String[] letter = new String[]{"↑", "#", "A", "B", "C", "D", "E", "F", "G",
            "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X",
            "Y", "Z"};

    private List<String> LETTERS = Arrays.asList(letter);

    private TextPaint mPaint;
    private float mTextSize = 38;
    private int mNormalColor = 0x00000000;
    private int mTouchColor = 0x00ff0000;

    private float cellHeight;// 单元格高
    private float cellWidth;// 单元格宽

    private int touchIndex = -1;//触摸点

    public interface OnLetterUpdateListener {
        void onLetterUpdateListener(String letter);
    }

    private OnLetterUpdateListener mListener;

    /**
     * 设置字母监听
     */
    public void setOnLetterUpdateListener(OnLetterUpdateListener listener) {
        mListener = listener;
    }

    public QuickIndexBar(Context context) {
        this(context, null);
    }

    public QuickIndexBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public QuickIndexBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.QuickIndexBar);
        int n = a.getIndexCount();
        for (int i = 0; i < n; i++) {
            if (i == R.styleable.QuickIndexBar_textSize) {
                mTextSize = a.getDimension(R.styleable.QuickIndexBar_textSize, mTextSize);
            } else if (i == R.styleable.QuickIndexBar_normalColor) {
                mNormalColor = a.getColor(R.styleable.QuickIndexBar_normalColor, mNormalColor);
            } else if (i == R.styleable.QuickIndexBar_touchColor) {
                mTouchColor = a.getColor(R.styleable.QuickIndexBar_normalColor, mTouchColor);
            }
        }
        a.recycle();

        mPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);// 去锯齿
        mPaint.setColor(mNormalColor);
        mPaint.setTypeface(Typeface.DEFAULT_BOLD);// 粗体
    }

    public void setLetters(List<String> letters) {
        this.LETTERS = letters;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int size = LETTERS.size();
        for (int i = 0; i < size; i++) {
            String text = LETTERS.get(i); // 计算xy坐标 //获取文本的宽度
            mPaint.setTextSize(mTextSize);
            mPaint.measureText(text);
            int x = (int) (cellWidth / 2.0f - mPaint.measureText(text) / 2.0f);
            // 获取文本的高度
            Rect rect = new Rect();
            // 文本所在的矩形，创建空矩形
            mPaint.getTextBounds(text, 0, text.length(), rect);
            // 填充文本
            int textHeight = rect.height();// 获取文本高度
            int y = (int) (cellHeight / 2.0f + textHeight / 2.0f + cellHeight * i);
            // 当字母被选中，使用不同颜色画出来，根据需求改变画笔的属性就可以了
            if (i == touchIndex) {
                mPaint.setColor(mTouchColor);
                mPaint.setFakeBoldText(true);
            }
            canvas.drawText(text, x, y, mPaint);

            mPaint.reset();
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh); // 测量出view的宽
        cellWidth = getMeasuredWidth(); // 测量view高度除以格子总数
        cellHeight = getMeasuredHeight() / LETTERS.size();
    }

    // 设置被选中项
    public void setTouchIndex(int touchIndex) {
        this.touchIndex = touchIndex;
        invalidate();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int index;
        int size = LETTERS.size();
        switch (MotionEventCompat.getActionMasked(event)) {
            case MotionEvent.ACTION_DOWN: // 所在view中点击的Y坐标（屏幕中的绝对坐标），换算成位置
                index = (int) (event.getY() / cellHeight);
                if (index >= 0 && index < size) {
                    if (index != touchIndex) {
                        if (mListener != null) {
                            mListener.onLetterUpdateListener(LETTERS.get(index));
                            touchIndex = index;
                        }
                    }
                }
                break;
            case MotionEvent.ACTION_MOVE:
                index = (int) (event.getY() / cellHeight);
                if (index >= 0 && index < size) {
                    if (index != touchIndex) {
                        if (mListener != null) {
                            mListener.onLetterUpdateListener(LETTERS.get(index));
                            touchIndex = index;
                        }
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        // 重绘，被选中字母颜色变化
        invalidate();
        // 拦截事件并处理
        return true;
    }


}